﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Transactions;
using T.FAS.Runtime.Metadata.Entity.Page;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Page.Dao
{
    class WRPRouterDao : DaoService<FasMenu>
    {
        string dbName;

        public new ITDatabase DB
        {
            get
            {
                return DatabaseHelper.GetDatabase(dbName);
            }
        }
        public WRPRouterDao(string _dbName)
        {
            dbName = _dbName;
        }
        /// <summary>
        /// 获取关键应用
        /// </summary>
        /// <param name="keyAppName"></param>
        /// <returns></returns>
        public string GetKeyAppName(string keyAppName)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(DB.MakeInParam("@keyAppName", TDbDataType.VarChar, keyAppName));
            if (DB.DbType == TDbType.MySql)
            {
                string keyAppSql = @"SELECT ApplicationName FROM sys_applicationname WHERE SubSystemId=@keyAppName";
                var ds = DB.ExecuteScalar(keyAppSql, paramsList.ToArray());
                if (!string.IsNullOrEmpty(Convert.ToString(ds)))
                {
                    keyAppName = Convert.ToString(ds);
                }
            }
            paramsList.Add(DB.MakeInParam("@FuncId", TDbDataType.VarChar, keyAppName + ".fas"));
            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
            {
                string sqDelete = "DELETE FROM WRPFuncPackage WHERE FuncId=@FuncId";
                DB.ExecSqlStatement(sqDelete, paramsList.ToArray());
                string insertSql = "INSERT INTO wrpfuncpackage (FuncId, FuncName, ApplicationName, JSName, Creator, CreateTime, Modifier, ModifyTime)  " +
                    $" Values (@FuncId,'fas',@keyAppName,'fas','T','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}','T','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
                var r = DB.ExecSqlStatement(insertSql, paramsList.ToArray());
                scope.Complete();
            }
            return keyAppName;
        }
        /// <summary>
        /// 删除路由
        /// </summary>
        /// <param name="router"></param>
        public void DeleteRouter(string router)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(DB.MakeInParam("@Router", TDbDataType.VarChar, router));
            string sqDelete = "DELETE FROM WRPFuncPackageRouter WHERE Router=@Router";
            DB.ExecSqlStatement(sqDelete, paramsList.ToArray());
        }
        /// <summary>
        /// 创建路由
        /// </summary>
        /// <param name="funcId"></param>
        /// <param name="Id"></param>
        /// <param name="router"></param>
        /// <param name="routerName"></param>
        /// <param name="mvcUrl"></param>
        public void CreateRouter(string funcId, string Id,string router,string routerName,string mvcUrl,string mvcAppName = "fas",string crossType = "")
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(DB.MakeInParam("@FuncId", TDbDataType.VarChar, funcId));
            paramsList.Add(DB.MakeInParam("@Id", TDbDataType.VarChar, Id));
            paramsList.Add(DB.MakeInParam("@Router", TDbDataType.VarChar, router));
            paramsList.Add(DB.MakeInParam("@RouterName", TDbDataType.VarChar, routerName));
            paramsList.Add(DB.MakeInParam("@MvcUrl", TDbDataType.VarChar, mvcUrl));
            paramsList.Add(DB.MakeInParam("@mvcAppName", TDbDataType.VarChar, mvcAppName));
            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
            {
                string sqDelete = "DELETE FROM WRPFuncPackageRouter WHERE ID=@Id OR (FuncId=@FuncId AND Router=@Router)";
                DB.ExecSqlStatement(sqDelete, paramsList.ToArray());

                string insertSql = "Insert into WRPFuncPackageRouter (Id,FuncId,IsMainPage,Router,RouterName,VueFileName,CrossApplication,CrossType,MvcAppName,MvcUrl,Sequence,ResourceInfoID,Version) " +
                    " Values (@Id,@FuncId,'0',@Router,@RouterName,'','1','Vue',@mvcAppName,@MvcUrl,0,'',1 )";
                if (crossType == "Fas")
                {
                    insertSql = "Insert into WRPFuncPackageRouter (Id,FuncId,IsMainPage,Router,RouterName,VueFileName,CrossApplication,CrossType,MvcAppName,MvcUrl,Sequence,ResourceInfoID,Version) " +
                    " Values (@Id,@FuncId,'0',@Router,@RouterName,'','1','Fas','',@MvcUrl,0,'',2 )";
                }
                var r = DB.ExecSqlStatement(insertSql, paramsList.ToArray());
                scope.Complete();
            }
        }
    }
}
